package com.mission.schedule.my160920.picker;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import com.mission.schedule.cutimage.Log;
import com.mission.schedule.my160920.kankan.wheel.widget.ArrayWheelAdapter;
import com.mission.schedule.my160920.kankan.wheel.widget.DateObject;
import com.mission.schedule.my160920.kankan.wheel.widget.OnWheelChangedListener;
import com.mission.schedule.my160920.kankan.wheel.widget.OnWheelChangedListener2;
import com.mission.schedule.my160920.kankan.wheel.widget.StringWheelAdapter;
import com.mission.schedule.my160920.kankan.wheel.widget.WheelView;
import com.mission.schedule.my160920.kankan.wheel.widget.WheelView2;

import java.util.ArrayList;
import java.util.Calendar;

/**
 * 自定义的时间选择器
 * @author sxzhang
 *
 */
public class TimePicker2 extends LinearLayout{

	private Calendar calendar = Calendar.getInstance();
	private WheelView2 hours, mins; //Wheel picker
	private OnChangeListener onChangeListener; //onChangeListener
	private final int MARGIN_RIGHT = 100;
	String[] numberList;//调整文字右端距离
	String[] type;
	int hour = 1,minute = 1;
	//Constructors
	public TimePicker2(Context context) {
		super(context);
		init(context);
	}

	public TimePicker2(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	/**
	 * 初始化
	 * @param context
	 */
	private void init(Context context){
		numberList = new String[24];
		type = new String[60];
		int hourItem = 0;
		int muItem = 0;

		for (int i = 0; i < 24; i++) {
			if (i < 9){
				numberList[i] = "0"+(i+1)+"";
			}else if(i >=9 && i < 23){
				numberList[i] = (i+1)+"";
			}else {
				numberList[i] = "00";
			}

		}

		for (int i = 0; i < 60; i++) {
			if (i == 59) {
				type[i] = "00";
			}else if(i < 9){
				type[i] = "0"+(i + 1);
			}else {
				type[i] = (i + 1) + "";
			}
		}

		hour = calendar.get(Calendar.HOUR_OF_DAY) - 1;
		minute = calendar.get(Calendar.MINUTE) - 1;


		//小时选择器
		hours = new WheelView2(context);
		LayoutParams lparams_hours = new LayoutParams(120,LayoutParams.WRAP_CONTENT);
		lparams_hours.setMargins(0, 0, MARGIN_RIGHT, 0);
		hours.setLayoutParams(lparams_hours);
		hours.setAdapter(new ArrayWheelAdapter(numberList, numberList.length));
		hours.setVisibleItems(5);
		hours.setCurrentItem(hour);
		hours.setCyclic(true);
		hours.addChangingListener(onHoursChangedListener);
		addView(hours);

		//分钟选择器
		mins = new WheelView2(context);
		mins.setLayoutParams(new LayoutParams(120,LayoutParams.WRAP_CONTENT));
		mins.setAdapter(new ArrayWheelAdapter(type,type.length));
		mins.setVisibleItems(5);
		mins.setCurrentItem(minute);
		mins.setCyclic(true);
		mins.addChangingListener(onMinsChangedListener);
		addView(mins);
	}

	public void setNowTime(){
		hours.setCurrentItem(hour);
		mins.setCurrentItem(minute);
	}


	public void setcuitem1(int item){
		hours.setCurrentItem(item);
	}

	public void setcuitem2(int item){
		mins.setCurrentItem(item);
	}


	//listeners
	private OnWheelChangedListener2 onHoursChangedListener = new OnWheelChangedListener2(){
		@Override
		public void onChanged(WheelView2 hours, int oldValue, int newValue) {
			calendar.set(Calendar.HOUR_OF_DAY, newValue);
			change();
		}
	};
	private OnWheelChangedListener2 onMinsChangedListener = new OnWheelChangedListener2(){
		@Override
		public void onChanged(WheelView2 mins, int oldValue, int newValue) {
			calendar.set(Calendar.MINUTE, newValue);
			change();
		}
	};

	/**
	 * 滑动改变监听器回调的接口
	 */
	public interface OnChangeListener {
		void onChange(String hour, String munite);
	}

	/**
	 * 设置滑动改变监听器
	 * @param onChangeListener
	 */
	public void setOnChangeListener(OnChangeListener onChangeListener){
		this.onChangeListener = onChangeListener;
	}

	/**
	 * 滑动最终调用的方法
	 */
	private void change(){
		if(onChangeListener!=null){
			onChangeListener.onChange(getNumber(), getType());
		}
	}


	/**
	 * 获取小时
	 * @return
	 */
	public String getNumber(){
		return numberList[hours.getCurrentItem()];
	}

	/**
	 * 获取分钟
	 * @return
	 */
	public String getType(){
		return type[mins.getCurrentItem()];
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}
}
